package cn.otrue.patienthealthmanager.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.HealthApplication;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.HealthPlanActivity;
import cn.otrue.patienthealthmanager.bean.DrugRemindModel;
import cn.otrue.patienthealthmanager.bean.PatientPlanModel;
import cn.otrue.patienthealthmanager.bean.PressureRemindModel;
import cn.otrue.patienthealthmanager.bean.SugarTemplateMonitorDetail;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.CalendarViewPlan;
import cn.otrue.patienthealthmanager.view.SwitchButton;

import com.google.gson.reflect.TypeToken;

/**
 * 健康计划Adapter
 * 
 * @author G
 * 
 */
public class MainPlanAdapter extends BaseAdapter {

	private List<PatientPlanModel> data;
	private LayoutInflater inflater;
	private Context context;

	public MainPlanAdapter(Context context, List<PatientPlanModel> data) {
		super();
		this.context = context;
		this.data = data;
		inflater = LayoutInflater.from(this.context);
	}

	private SharedPreferences mySharedPreferences = HealthApplication
			.getInstance().getSharedPreferences("calendar",
					Activity.MODE_PRIVATE);

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	private Map<String, String> getMapByModel(PatientPlanModel eachPlan,
			Map<String, String> plan) {
		Map<String, Object> planTimeMap = eachPlan.getPlanTimeMap();

		if (planTimeMap.get("bloodsugar") != null) {
			Map<String, String> bloodsugarTime = (Map<String, String>) ((Map<String, Object>) planTimeMap
					.get("bloodsugar")).get("time");
			if (bloodsugarTime != null) {
				if (!TextUtils.isEmpty(bloodsugarTime.get("end"))) {
					plan.put("bloodsugar_endtime", bloodsugarTime.get("end"));

				}
				if (!TextUtils.isEmpty(bloodsugarTime.get("begin"))) {
					plan.put("bloodsugar_begintime",
							bloodsugarTime.get("begin"));

				}
			}
		}
		if (planTimeMap.get("bloodsugarweeks") != null) {
			List<SugarTemplateMonitorDetail> sugarTime = HttpServer.gson
					.fromJson(HttpServer.gson.toJson(planTimeMap
							.get("bloodsugarweeks")),
							new TypeToken<List<SugarTemplateMonitorDetail>>() {
							}.getType());
			String sugarWeek = "";
			for (int i = 0; i < sugarTime.size(); i++) {
				if ((sugarTime.get(i).getAfter_breakfast()
						| sugarTime.get(i).getAfter_dinner()
						| sugarTime.get(i).getAfter_lunch()
						| sugarTime.get(i).getBefore_dinner()
						| sugarTime.get(i).getBefore_drawn()
						| sugarTime.get(i).getBefore_lunch() | sugarTime.get(i)
						.getBefore_sleep()) != 0) {
					sugarWeek = sugarWeek + sugarTime.get(i).getWeekend() + ",";
				}
			}
			plan.put("bloodpressureweeks", sugarWeek);

		}

		if (planTimeMap.get("bloodpressure") != null) {
			Map<String, String> bloodpressureTime = (Map<String, String>) ((Map<String, Object>) planTimeMap
					.get("bloodpressure")).get("time");
			if (bloodpressureTime != null) {
				if (!TextUtils.isEmpty(bloodpressureTime.get("end"))) {
					plan.put("bloodpressure_endtime",
							bloodpressureTime.get("end"));

				}
				if (!TextUtils.isEmpty(bloodpressureTime.get("begin"))) {
					plan.put("bloodpressure_begintime",
							bloodpressureTime.get("begin"));

				}
			}
		}

		if (planTimeMap.get("bloodpressureweeks") != null) {
			List<PressureRemindModel> bloodpressureTime = HttpServer.gson
					.fromJson(HttpServer.gson.toJson(planTimeMap
							.get("bloodpressureweeks")),
							new TypeToken<List<PressureRemindModel>>() {
							}.getType());
			String bloodpressureWeek = "";
			for (int i = 0; i < bloodpressureTime.size(); i++) {

				bloodpressureWeek = bloodpressureWeek
						+ bloodpressureTime.get(i).getDrugweek() + ",";

			}
			plan.put("bloodpressure_week", bloodpressureWeek);

		}

		if (planTimeMap.get("drugplan") != null) {
			Map<String, String> drugTime = (Map<String, String>) ((Map<String, Object>) planTimeMap
					.get("drugplan")).get("time");
			if (drugTime != null) {
				if (!TextUtils.isEmpty(drugTime.get("end"))) {
					plan.put("drugplan_endtime", drugTime.get("end"));

				}
				if (!TextUtils.isEmpty(drugTime.get("begin"))) {
					plan.put("drugplan_begintime", drugTime.get("begin"));

				}
			}
		}

		if (planTimeMap.get("drugplanweeks") != null) {
			List<DrugRemindModel> drugTime = HttpServer.gson.fromJson(
					HttpServer.gson.toJson(planTimeMap.get("drugplanweeks")),
					new TypeToken<List<DrugRemindModel>>() {
					}.getType());
			String drugWeek = "";
			for (int i = 0; i < drugTime.size(); i++) {

				drugWeek = drugWeek + drugTime.get(i).getDrugweek() + ",";

			}
			plan.put("drugplan_week", drugWeek);

		}
		plan.put("bloodpressureId", eachPlan.getBloodpressureId());
		plan.put("bloodsugarId", eachPlan.getBloodsugarId());
		plan.put("drugplanId", eachPlan.getDrugplanId());

		return plan;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		mySharedPreferences.edit().clear().commit();
		final PatientPlanModel eachPlan = data.get(arg0);
		Map<String, String> plan = new HashMap<String, String>();
		plan = getMapByModel(eachPlan, plan);

		mySharedPreferences.edit()
				.putString("plan", HttpServer.gson.toJson(plan)).commit();

		View view = inflater.inflate(R.layout.item_main_plan, null);

		final LinearLayout ll_show_plan = (LinearLayout) view
				.findViewById(R.id.ll_show_plan);
		TextView tv_main_plan_name = (TextView) view
				.findViewById(R.id.tv_main_plan_name);
		CheckBox cb_main_plan_ring = (CheckBox) view
				.findViewById(R.id.cb_main_plan_ring);

		ImageView iv_plan_lastMonth = (ImageView) view
				.findViewById(R.id.iv_plan_lastMonth);

		ImageView iv_plan_nextMonth = (ImageView) view
				.findViewById(R.id.iv_plan_nextMonth);
		final TextView tv_plan_date = (TextView) view
				.findViewById(R.id.tv_plan_date);

		final CalendarViewPlan ll_main_plan_calendar = (CalendarViewPlan) view
				.findViewById(R.id.cvp_main_plan_calendar);

		tv_plan_date.setText(ll_main_plan_calendar.getYearAndmonth());

		iv_plan_lastMonth.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				tv_plan_date.setText(ll_main_plan_calendar.clickLeftMonth());

			}
		});

		iv_plan_nextMonth.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				tv_plan_date.setText(ll_main_plan_calendar.clickRightMonth());

			}
		});
		ll_main_plan_calendar.setOnTouchListener(new OnTouchListener() {
			private float x;

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {

				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {

					x = arg1.getX();
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {

					float daltX = Math.abs(x - arg1.getX());
					if (daltX < 20) {

						Intent intent = new Intent();
						intent.setClass(context, HealthPlanActivity.class);
						intent.putExtra("type", MyConst.SHOW_PLAN);
						intent.putExtra("bloodpressureId",
								eachPlan.getBloodpressureId());

						intent.putExtra("bloodpressure_begintime",
								eachPlan.getBloodpressure_begintime());
						intent.putExtra("bloodpressure_endtime",
								eachPlan.getBloodpressure_endtime());
						intent.putExtra("blood_pressure_note",
								eachPlan.getBlood_pressure_note());

						intent.putExtra("bloodsugarId",
								eachPlan.getBloodsugarId());

						intent.putExtra("bloodsugar_begintime",
								eachPlan.getBloodsugar_begintime());
						intent.putExtra("bloodsugar_endtime",
								eachPlan.getBloodsugar_endtime());

						intent.putExtra("drugplanId", eachPlan.getDrugplanId());
						intent.putExtra("drugplan_begintime",
								eachPlan.getDrugplan_begintime());
						intent.putExtra("drugplan_endtime",
								eachPlan.getDrugplan_endtime());

						intent.putExtra("doctorName", eachPlan.getDoctorname());
						intent.putExtra("doctorId", eachPlan.getDoctorId());
						// if (eachPlan.getDoctorId().equals(MyConst.ACCOUNT)) {
						// intent.putExtra("isChange", "0");
						// } else {
						// intent.putExtra("isChange", "1");
						context.startActivity(intent);
					}

				}
				return true;
			}
		});

		final SwitchButton sb_main_switch = (SwitchButton) view
				.findViewById(R.id.sb_main_switch);
		if (eachPlan.getDoctorId().equals(MyConst.getACCOUNT())) {
			tv_main_plan_name.setText("我的计划");
		} else {
			tv_main_plan_name.setText(eachPlan.getDoctorname());
		}
		if (eachPlan.isRing()) {
			cb_main_plan_ring.setChecked(true);
		} else {
			cb_main_plan_ring.setChecked(false);
		}

		if (eachPlan.getState() != null
				&& eachPlan.getState().equals(MyConst.OPEN_PLAN)) {
			sb_main_switch.openButton(false);
			ll_show_plan.setVisibility(View.GONE);
		} else {
			sb_main_switch.openButton(true);
			ll_show_plan.setVisibility(View.VISIBLE);
		}

		sb_main_switch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				sb_main_switch.changeOpen();
				if (sb_main_switch.open) {
					ll_show_plan.setVisibility(View.VISIBLE);
				} else {
					ll_show_plan.setVisibility(View.GONE);
				}

			}
		});

		return view;
	}
}
